package com.pj.utils;

import com.pj.entity.Book;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: szy
 * @program: pj-novel
 * @description: 爬虫解析器
 * @create: 2022-11-08 11:36
 **/

@Resource

public class CrawlParser {
    @Value("${craw-source.url}")
    private static String sourceUrl;

    private static final Logger logger = LoggerFactory.getLogger(CrawlParser.class);

    public static List<Book> getByHttpClientWithChrome(String url, String category, Integer sourceId) {
        try {
            String body = HttpUtil.getByHttpClientWithChrome(url);
            List<Book> book = analysisCatalogueHtml(body,category,sourceId);
            //成功获得html内容,
            return book;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取html失败~");
        }
        return null;
    }

    /**
     * 解析书籍html
     * @param body
     * @return
     */
    public static List<Book> analysisCatalogueHtml(String body,String category,Integer sourceId){
        List<Book> bookList = new ArrayList();
        Document document = Jsoup.parseBodyFragment(body);
        Elements itemClassDetail = document.getElementsByClass("item");
        for (Element element : itemClassDetail) {
            Book book = new Book();
            String bookAuthor = element.getElementsByTag("span").text();
//            String bookIntroduce = element.getElementsByTag("dd").text();//小说简介
            String bookUrl = element.getElementsByTag("a").get(0).attributes().get("href");
            Attributes bookAttr = element.getElementsByTag("img").get(0).attributes();
            String picUrl = bookAttr.get("src");
            String bookName = bookAttr.get("alt");
            book.setBookName(bookName);//书名

            book.setCatId(BookDetailUtils.catNameConvertCatId(category));//分类id
            book.setPicUrl(picUrl);//封面
            book.setAuthorName(bookAuthor);//作者名称
            book.setCrawlSourceId(sourceId);//爬虫源ID
            book.setCrawlBookId(bookUrl.split("/")[2]);
            bookList.add(book);
        }
        analysisChapterHtml(bookList);
        return null;
    };

    public static void analysisChapterHtml(List<Book> bookList){
        for (Book book : bookList) {
            String chapterHtmlBody = HttpUtil.getByHttpClientWithChrome(sourceUrl + book.getCrawlBookId());
            Document chapterDoc = Jsoup.parseBodyFragment(chapterHtmlBody);
            // 状态
            Elements samll = chapterDoc.body().getElementsByClass("samll");
            String span = samll.get(1).getElementsByTag("span").text();
            Elements dd = chapterDoc.body().getElementsByClass("intro");
            Elements listmain = chapterDoc.body().getElementsByClass("listmain");
            System.out.println("~~~~~~~~~~~~~~");


        }
    };
}
